// 检查登录状态
const checkLoginStatus = () => {
    // const openid = uni.getStorageSync('openids');
    // return openid ? true : false
    return false
}

// 需要登录的页面路径列表
const needLoginPages = [
    '/pages/diary/add'
]

// 统一的拦截处理函数
const handleInterceptor = (params: any) => {
    console.log("handlerInterceptor")
    const url = params.url.split('?')[0]
    if (needLoginPages.includes(url) && !checkLoginStatus()) {
        uni.navigateTo({
            url: '/pages/auth/login'
        })
        return false
    }
    uni.navigateTo({
        url: '/pages/auth/login'
    })
    return false
}

// 路由拦截器
const setupRouteGuard = () => {
    ["navigateTo", "redirectTo", "reLaunch", "switchTab"].forEach(method => {
        uni.addInterceptor(method, {
            invoke: handleInterceptor
        })
    })
}

export default setupRouteGuard